package com.test.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.test.domain.AdminUser;
import com.test.service.AdminUserService;

@Controller
public class ThymeleafController {
	@Autowired
	public AdminUserService adminUserService;
	
    @RequestMapping("/login")
	public String hello(AdminUser user, ModelAndView mv, HttpServletRequest request, HttpSession session, Map<String,Object> map) {
    	String name = Thread.currentThread().getStackTrace().getClass().getTypeName();
    	System.out.println(name);
    	System.out.println("进入登录页面");
    	
    	if("admin".equals(user.getUserName()) && "123456".equals(user.getPassword())){
            //登陆成功，防止表单重复提交，可以重定向到主页
            session.setAttribute("loginUser",user.getUserName());
            return "redirect:/index.html";
        }else{
            //登陆失败
            map.put("msg","用户或名密码错误");
            return  "login";
        }
    	/*AdminUser loginer = adminUserService.login(user);
    	if(loginer != null) {
    		request.getSession().setAttribute("loginer",loginer);
            System.out.println("成功！！");
            mv.setViewName("index");
    	}else {
    		System.out.println("失败！！");
    		mv.setViewName("login");
    		
    	}
    	return mv;*/

	}
    
    @ResponseBody
    @RequestMapping("/test")
	public Object test() {
    	return adminUserService.getAll();
	}

}